// pages/my/my.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    color:'#A3A4AA',
    name:'',
    phone:'',
    region:[],
    detailAddress:'',
    userPhone:wx.getStorageSync('phone'),
  },
  formSubmit(e){
    let data=e.detail.value;
    if(data.name==''){
      wx.showToast({
        title: "请输入收件人",
        icon: 'none',
      });
      return ;
    }
    let regex = /^1[3456789]\d{9}$/;
    if(!regex.test(data.phone)){
      wx.showToast({
        title: "请输入正确的手机号码",
        icon: 'none',
      });
      return ;
    }
    if(this.data.region.length==0){
      wx.showToast({
        title: "请选择所在地区",
        icon: 'none',
      });
      return ;
    }
    if(data.detailAddress==''){
      wx.showToast({
        title: "请输入详细地址",
        icon: 'none',
      });
      return ;
    }
    let province=this.data.region[0];
    let city=this.data.region[1];
    let county=this.data.region[2];
    let id=this.data.id;
    let _this=this;
    if(id){
      wx.request({
        url: app.globalData.onlineUrl+'/addressNew/update', 
        method: 'POST',
        data: {
          province:province,
          city:city,
          phone:data.phone,
          county:county,
          detailAddress:data.detailAddress,
          name:data.name,
          id:id
        },
        success: function(res) {
          wx.navigateBack({
            url: '/pages/address/address'
          })
        },
        fail: function(err) {
        },
        complete: function() {}
      });
    }else{
      wx.request({
        url: app.globalData.onlineUrl+'/addressNew/add', 
        method: 'POST',
        data: {
          province:province,
          city:city,
          phone:data.phone,
          county:county,
          detailAddress:data.detailAddress,
          name:data.name,
          userPhone:_this.data.userPhone?wx.getStorageSync('phone'):data.phone
        },
        success: function(res) {
          wx.navigateBack({
            url: '/pages/address/address'
          })
        },
        fail: function(err) {
        },
        complete: function() {}
      });
    }
    
  },
  RegionChange: function(e) {
    this.setData({
      region: e.detail.value,
      color:'#282828'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.arryStr){
      let arry=options.arryStr.split(',')
      this.setData({
        id:arry[0],
        color:'#282828',
        name:arry[5],
        phone:arry[6],
        region:[arry[1],arry[2],arry[3]],
        detailAddress:arry[4],
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(!this.data.userPhone){
      this.data.userPhone=wx.getStorageSync('phone')
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})